package Program;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Mode {
    static Date date=null;
    static List<Deplete> depletes = new ArrayList<>();
    static List<TaoCan> taoCans = new ArrayList<>();
    static List<Account> accounts = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    static Random r = new Random();
    static final TaoCan taoCans1 = new TaoCan("普通套餐", 50, 0, 50, 58);
    static final TaoCan taoCans2 = new TaoCan("VIP套餐", 0, 5, 0, 68);
    static final TaoCan taoCans3 = new TaoCan("SVIP套餐", 200, 1, 0, 78);

    public static void main(String[] args) {
        taoCans.add(taoCans1);
        taoCans.add(taoCans2);
        taoCans.add(taoCans3);
        while (true) {
            System.out.println("***********欢迎使用中国移动业务大厅**********");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.使用搜搜");
            System.out.println("4.话费充值");
            System.out.println("5.资费说明");
            System.out.println("6.退出系统");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                   // DengLu(3);
                   login();
                    break;
                case 2:
                    ZhuCe();
                    break;
                case 3:
                    //使用哈哈
                    System.out.println("请输入当前卡号");
                    String str = sc.next();
                    useHaHa(str);
                    break;
                case 4:
                    recharge();
                    break;
                case 5:
                    try {
                        BufferedReader reader = new BufferedReader
                                (new FileReader("src/Program/套餐资费说明.txt"));
                        String st;
                        while ((st = reader.readLine()) != null) {
                            System.out.println(st);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case 6:
                    System.out.println("退出成功，欢迎下次光临！");
                    return;
            }
        }

    }

    public static void ZhuCe() {
        System.out.println("========注册界面=========");
        List<String> Number = new ArrayList<>();
        for (int j = 1; j <= 9; j++) {
            StringBuilder num = new StringBuilder("132");
            for (int i = 0; i < 6; i++) {
                int n = r.nextInt(10);
                String m = n + "";
                num.append(m);
            }
            String nums = num.toString();
            for (Account account : accounts) {
                if (nums.equals(account.getPhone())) {
                    j--;
                } else {
                    Number.add(nums);
                }
            }
            num.delete(0, 11);
            Number.add(nums);
            System.out.println(j + "." + Number.get(j - 1));
        }
        System.out.println("请选择卡号");
        int orders = sc.nextInt();
        System.out.println("请选择套餐序号");
        System.out.println("1.普通套餐");
        System.out.println("2.VIP套餐");
        System.out.println("3.SVIP套餐");
        int TC = sc.nextInt();
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入密码");
        String MiMa = sc.next();
        String s = MD5Util.getMD5Str(MiMa);
        Map<Integer, TaoCan> Breakfast = new LinkedHashMap<>();
        Breakfast.put(1, taoCans1);
        Breakfast.put(2, taoCans2);
        Breakfast.put(3, taoCans3);
        System.out.println("请输入预存话费金额");
        double Moneys = sc.nextDouble();
        TaoCan tc = Breakfast.get(TC);
        while (true) {
            if (tc.money > Moneys) {
                System.out.println("你存入的花费不足，请重新充值");
                Moneys = sc.nextDouble();
            } else {
                System.out.println("充值成功" + "余额" + (Moneys - tc.money));
                System.out.println(tc);
                break;
            }
        }
        System.out.println("注册成功");
        System.out.println("卡号：" + Number.get(orders - 1) + " " + "用户名：" + name + " " +
                "密码：" + MiMa);
        Account account = new Account(Number.get(orders - 1), name,
                s, (Moneys - Breakfast.get(TC).money), Breakfast.get(TC), new Deplete());
        accounts.add(account);
        System.out.println(account);
    }
    public static boolean login(){
        if(date!=null){
            Date date2 =new Date();
           long hours= (date2.getTime()-date.getTime())/1000/3600;
           if(hours<=24){
               System.out.println("一天后才可以登录，还需要 "+(24-hours)+"小时才可以登录");
               return false;
           }
        }
        for (int i = 1; i < 4; i++) {
            System.out.println("请你输入电话号码");
            String telNo = sc.next();
            System.out.println("请输入密码");
            String pwd = sc.next();
            if(loginCheck(telNo, pwd)){
                return true;
            }
        }
        System.out.println("连续三次登录 失败");
        date =new Date();
        return false;

    }
    public static boolean loginCheck(String ph, String password) {
        for (Account account1 : accounts) {
            if (account1.getPhone().equals(ph) &&
                    account1.getPassword().equals(MD5Util.getMD5Str(password))) {
                System.out.println("登录成功");
                secondaryMenu(account1);
                return true;
            }
        }
        System.out.println("登录失败，请重试");
        return false;
    }
    /**
     *
     * @param account 账户
     */
    private static void secondaryMenu(Account account) {
        System.out.println("=========进入二级菜单============");
        System.out.println("1.套餐余量查询，2.打印消费详单，" +
                "3.套餐变更，4.办理退网.5.本月账单查询");
        System.out.println("请选择你需要的服务");
        System.out.println("请选择（输入1~5选择功能，其他键返回上一级）");
        int i = sc.nextInt();
        if (i == 1) {
            System.out.println("======套餐余量查询界面=========");
            leftDelept(account);
        } else if (i == 2) {
            System.out.println("======打印消费详单界面=========");
            // BufferedWriter writer=new BufferedWriter(new FileWriter(""))
            printInfo(account);
            for (Deplete deplete1 : depletes) {
                System.out.println(deplete1);
            }
        } else if (i == 3) {
            System.out.println("======套餐变更界面=============");
            System.out.println("请选择你要变更的套餐的序号：1.普通套餐，2.VIP套餐，3.SVIP套餐");
            int j = sc.nextInt();
            switch (j) {
                case 1:
                    if (account.getTaoCan().equals(taoCans1)) {
                        System.out.println("你当前的套餐就是普通套餐，不用变更");
                    } else {
                        if (taoCans1.money > account.getMoney()) {
                            System.out.println("你的手机余额不足，还再需要充值" +
                                    (taoCans1.money - account.getMoney()) + "元");
                        } else {
                            account.setTaoCan(taoCans1);
                        }
                        System.out.println("你现在的账户余额为" + account.getMoney());
                    }
                    System.out.println("你当前的套餐是：" + account.getTaoCan().type);
                    break;
                case 2:
                    if (account.getTaoCan().equals(taoCans2)) {
                        System.out.println("你当前的套餐就是VIP套餐，不用变更");
                    } else {
                        if (taoCans2.money > account.getMoney()) {
                            System.out.println("你的手机余额不足，还再需要充值" +
                                    (taoCans2.money - account.getMoney()) + "元");
                        } else {
                            account.setTaoCan(taoCans2);
                        }
                        System.out.println("你现在的账户余额为" + account.getMoney());
                    }
                    System.out.println("你当前的套餐是：" + account.getTaoCan().type);
                    break;
                case 3:
                    if (account.getTaoCan().equals(taoCans3)) {
                        System.out.println("你当前的套餐就是SVIP套餐，不用变更");
                    } else {
                        if (taoCans3.money > account.getMoney()) {
                            System.out.println("你的手机余额不足，还再需要充值" +
                                    (taoCans3.money - account.getMoney()) + "元");
                        } else {
                            account.setTaoCan(taoCans3);
                        }
                        System.out.println("你现在的账户余额为" + account.getMoney());
                    }
                    System.out.println("你当前的套餐是：" + account.getTaoCan().type);
                    break;
            }
        } else if (i == 4) {
            accounts.remove(account);
            System.out.println("退网成功，欢迎下次光临！");
        } else if (i == 5) {
            System.out.println("============本月账单查询界面========");
            //卡号，套餐月资费，合计（是套餐固定资费和超出）账户余额。
            System.out.println("你的卡号是" + account.getPhone());
            System.out.println("月套餐资费" + account.getTaoCan().money);
            if (account.getMoney() >= 0) {
                System.out.println("账户余额" + account.getMoney() + "元");
            } else {
                account.setMoney(-account.getMoney());
                System.out.println("欠费" + account.getMoney() + "元");
            }
        }
    }

    public static void recharge() {//充值话费；
        System.out.println("请你输入电话号码");
        String ph = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        for (Account account1 : accounts) {
            if (account1.getPhone().equals(ph) &&
                    account1.getPassword().equals(MD5Util.getMD5Str(password))) {
                System.out.println("进行充值");
                System.out.println("你当前的余额是" + account1.getMoney());
                System.out.println("请输入你的充值金额(充值金额一次不少于50元)");
                double d = sc.nextDouble();
                if (d >= 50) {
                    account1.setMoney(account1.getMoney() + d);
                    System.out.println("当前余额是" + account1.getMoney());
                    System.out.println("充值成功");
                } else {
                    System.out.println("充值金额小于50元，无法进行充值。");
                }
            }
            break;
        }
    }

    static Info info = null;

    /**
     *
     * @param str 用户密码
     */
    public static void useHaHa(String str) {//使用哈哈
        int result;
        for (Account account : accounts) {
            if (account.getPhone().equals(str)) {
                System.out.println("=======使用哈哈界面=======");
                List<Recording> recordings = new ArrayList<>();
                recordings.add(new Recording("通话", 90, "问候客户"));
                recordings.add(new Recording("通话", 30, "询问妈妈身体状况"));
                recordings.add(new Recording("信息", 5, "参与环境保护实施方案问卷调查"));
                recordings.add(new Recording("信息", 50, "通知朋友手机换号，法短信条"));
                recordings.add(new Recording("上网", 1, "和女友聊天时用流量"));
                recordings.add(new Recording("上网", 2, "晚上手机在线看韩剧，不留神睡着了！"));
                Recording recording = recordings.get(r.nextInt(recordings.size()));
                switch (recording.service) {
                    case "通话":
                        if (account.getTaoCan().time > 0) {
                            System.out.println("你正进行通话操作，通话时长为" + recording.count + "分钟" + recording.service);
                            result = account.getTaoCan().time - recording.count;
                            if (result >= 0) {
                                account.getTaoCan().time = result;
                                account.setDeplete(new Deplete(result, account.getTaoCan().message, account.getTaoCan().GB));
                            } else {
                                account.getTaoCan().time = 0;
                                account.setMoney(account.getMoney() + result * 0.2);
                                account.setDeplete(new Deplete(0, account.getTaoCan().message, account.getTaoCan().GB));
                            }
                            info = new Info(str, "通话", recording.count, new Date());
                            infos.add(info);
                        } else {
                            System.out.println("该套餐无法进行通话服务");
                        }
                        break;
                    case "信息":
                        if (account.getTaoCan().message > 0) {
                            System.out.println("你正进行信息操作，信息条数为" + recording.count + "条" + recording.service);
                            result = account.getTaoCan().message - recording.count;
                            if (result >= 0) {
                                account.getTaoCan().message = result;
                                account.setDeplete(new Deplete(account.getTaoCan().time, result, account.getTaoCan().GB));
                            } else {
                                account.setMoney(account.getMoney() + result * 0.1);
                                account.getTaoCan().message = 0;
                                account.setDeplete(new Deplete(account.getTaoCan().time, 0, account.getTaoCan().GB));
                            }
                            info = new Info(str, "信息", recording.count, new Date());
                            infos.add(info);
                        } else {
                            System.out.println("该套餐无法进行信息服务");
                        }
                        break;
                    case "上网":
                        if (account.getTaoCan().GB > 0) {
                            System.out.println("你正进行上网操作，流量消耗为" + recording.count + "G" + recording.service);
                            result = account.getTaoCan().GB - recording.count;
                            if (result >= 0) {
                                account.getTaoCan().GB = result;
                                account.setDeplete(new Deplete(account.getTaoCan().time, account.getTaoCan().message, result));
                            } else {
                                account.setMoney(account.getMoney() + result * 0.1);
                                account.getTaoCan().GB = 0;
                                account.setDeplete(new Deplete(account.getTaoCan().time, account.getTaoCan().message, 0));
                            }
                            info = new Info(str, "上网", recording.count, new Date());
                            infos.add(info);
                        } else {
                            System.out.println("该套餐无法进行上网服务");
                        }
                        break;
                }
            }
        }
    }

    /**
     *
     * @param account 账户
     */
    public static void leftDelept(Account account) {
        System.out.println("你的卡号是" + account.getPhone());
        System.out.println("套餐内剩余：");
        System.out.println(account.getDeplete());
    }

    static List<Info> infos = new ArrayList<>();

    /**
     * @param account 账户
     */
    public static void printInfo(Account account) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\" + account.getPhone() + ".txt"));
            writer.write("************" + account.getPhone() + "消费记录*****************");
            writer.write("\n序号\t\t类型\t\t数据 （通话(分钟)/上网（MB）/短信(条)\t\t时间）");
            int i = 0;
            for (Info info1 : infos) {
                i++;
                if (info1.numsph.equals(account.getPhone())) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    writer.write("\n" + i + "\t\t" + info1.scence + "\t\t" + info1.count+ "\t\t" + sdf.format(info1.time));
                    //换行
                    writer.newLine();
                    //刷新数据到磁盘
                    writer.flush();
                }
            }
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}